import { defineStore } from "pinia";
import { JxtechAuth, JxtechMenu } from "@/stores/interface/auth";
import { getAuthButtonListApi, getAuthMenuListApi } from "@/api/modules/login";
import { getFlatMenuList, getShowMenuList, getAllBreadcrumbList, filterMenuNewArr } from "@/utils";
import { getMenu } from "@/api/modules/org/manager-service";

export const useAuthStore = defineStore({
  id: "jxtech-auth",
  state: (): JxtechAuth => ({
    // 按钮权限列表
    authButtonList: {},
    // 菜单权限列表
    authMenuList: [],
    // 当前页面的 router name，用来做按钮权限筛选
    routeName: ""
  }),
  getters: {
    // 按钮权限列表
    authButtonListGet: state => state.authButtonList,
    // 菜单权限列表 ==> 这里的菜单没有经过任何处理
    authMenuListGet: state => state.authMenuList,
    // 菜单权限列表 ==> 左侧菜单栏渲染，需要剔除 isHide == true
    showMenuListGet: state => getShowMenuList(state.authMenuList),
    // 菜单权限列表 ==> 扁平化之后的一维数组菜单，主要用来添加动态路由
    flatMenuListGet: state => getFlatMenuList(state.authMenuList),
    // 递归处理后的所有面包屑导航列表
    breadcrumbListGet: state => getAllBreadcrumbList(state.authMenuList)
  },
  actions: {
    // Get AuthButtonList
    async getAuthButtonList() {
      // const { data } = await getAuthButtonListApi();
      // this.authButtonList = data;
    },
    // Get AuthMenuList
    async getAuthMenuList() {
      // const { data } = await getAuthMenuListApi();
      const { data } = await getMenu();
      const home: JxtechMenu = {
        attributes: {
          compId: "application",
          image: "icon-home",
          menuCode: "demo:appvue",
          menuOpenMode: null,
          order: null,
          pagePoint: false,
          url: "/home/index",
          vuePath: null
        },
        checked: false,
        children: [],
        id: Math.random(),
        isParent: false,
        level: 2,
        selectType: 0,
        state: "open",
        text: "首页"
      };
      data.unshift(home);
      this.authMenuList = data;
    },
    // Set RouteName
    async setRouteName(name: string) {
      this.routeName = name;
    },
    /**清除按钮-菜单 */
    clearAuth() {
      this.$reset();
    }
  }
});
